package org.zkzd.helloEasyQuery.controller;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zkzd.helloEasyQuery.entity.Company;
import org.zkzd.helloEasyQuery.entity.proxy.CompanyProxy;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RequestMapping("/company")
public class CompanyController {
    private final EasyEntityQuery entityQuery;

    @PostMapping("/save")
    public Long save(@RequestBody Company company){
        return entityQuery.insertable(company).executeRows();
    }

    @PostMapping("/saveBatch")
    public Long saveBatch(@RequestBody List<Company> companies){
        return entityQuery.insertable(companies).executeRows();
    }

    @DeleteMapping("/delete")
    public Long delete(@RequestBody Company company){
        return entityQuery.deletable(company).disableLogicDelete().executeRows();
    }

    @DeleteMapping("/deleteBatch")
    public Long deleteBatch(@RequestBody List<Company> companies){
        return entityQuery.deletable(companies).disableLogicDelete().executeRows();
    }

    @PutMapping("/update")
    public Long update(@RequestBody Company company){
        return entityQuery.updatable(company).executeRows();
    }

    @PutMapping("/updateBatch")
    public Long updateBatch(@RequestBody List<Company> companies){
        return entityQuery.updatable(companies).executeRows();
    }

    @GetMapping("/get")
    public Company getByName(@RequestParam String name){
        return entityQuery.queryable(Company.class).where(c -> c.name().like(name)).firstOrNull();
    }

    @GetMapping("/list")
    public List<Company> list(){
        return entityQuery.queryable(Company.class).includes(CompanyProxy::users).toList();
    }
}

